package com.tb.controller;

import com.tb.dubbo.IUserService;
import com.tb.pojo.User;
import com.tb.utils.Msg;
import com.tb.utils.ReturnList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService iUserService;
    /**
     * @api {post} /user/addAdmin 管理员账号添加    王玺玉
     * @apiDescription 管理员账号添加&nbsp;&nbsp;<span style="color:#D0254E"><i>[*需要在管理员登陆状态下访问]</i></span>
     * @apiGroup User API
     *
     *
     * @apiParam {String}  userName 用户名
     * @apiParam {String}  userPass 用户密码
     * @apiParam {String}  userStatus 账号状态 0：锁定 1：解锁
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     * 	        "userName": "duguqiubai",
     * 	        "userPass": "123456",
     * 	        "userStatus":1
     *      }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccess  {int} code 返回状态码 0:正常 1:错误
     * @apiSuccess  {string} msg 返回错误信息
     * @apiSuccess  {Object} data 返回的成功信息
     * @apiSuccessExample {json} 正确返回值:
     *      {
     *     "code": 0,
     *     "msg": "",
     *      "data": "SUCCESS"
     *    }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 1,
     *     "msg": "failed",
     *     "data": null
     * }
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 999,
     *     "msg": "登录超时",
     *     "data": null
     * }
     * @apiErrorExample {json} 错误   ：status非0和非1
     *  {
     *   "code": 1,
     *   "msg": "用户账号状态码有误",
     *   "data": null
     * }
     *
     * @apiSampleRequest /user/addAdmin
     */
    @PostMapping("/addAdmin")
    public Msg addAdmin(@RequestBody User user, HttpServletRequest request){
        HttpSession session = request.getSession();
        try {
            user.setRolerId(2);
        }catch (Exception e){
            return Msg.error(1,"参数有误");
        }
        try {
            User user1= (User) session.getAttribute("AdminUser");
            user1.getId();
        }catch (Exception e){
            return Msg.error(999,"登录超时");
        }
        Integer userStatus = user.getUserStatus();

        if (userStatus==null){
            user.setUserStatus(0);
        }else if (!(0==userStatus||1==userStatus)){
            return Msg.error(1,"用户账号状态码有误");
        }

        boolean b = iUserService.addAdmin(user);
        if (true==b){
            return Msg.success("SUCCESS");
        }
        return Msg.error(1,"failed");
    }

    /**
     * @api {put} /user/updateAdmin 管理员账号修改   王玺玉
     * @apiDescription 管理员账号修改&nbsp;&nbsp;<span style="color:#D0254E"><i>[*需要在管理员登陆状态下访问]</i></span>
     * @apiGroup User API
     *
     *  @apiParam {String}  userId 用户id   必须传 其余可选
     *  @apiParam {String}  userName 用户名
     *  @apiParam {String}  userPass 用户密码
     *  @apiParam {String}  userStatus 账号状态
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     * 	        "userId":"12344567788",
     *          "userName": "dier",
     * 	        "userPass": "123456",
     * 	        "userStatus":1
     *      }
     *
     * @apiVersion 1.0.0
     *
     *@apiSuccess  {int} code 返回状态码 0:正常 1:错误
     *@apiSuccess  {string} msg 返回错误信息
     *@apiSuccess  {Object} data 返回的成功信息
     *
     * @apiSuccessExample {json} 正确返回值:
     *      {
     *     "code": 0,
     *     "msg": "",
     *      "data": "SUCCESS"
     *    }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 1,
     *     "msg": "failed",
     *     "data": null
     * }
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 999,
     *     "msg": "登录超时",
     *     "data": null
     * }
     *
     *  @apiErrorExample {json} 错误   ：status非0和非1
     *  {
     *   "code": 1,
     *   "msg": "用户账号状态码有误",
     *   "data": null
     * }
     *
     * @apiSampleRequest /user/updateAdmin
     */
    @PutMapping("/updateAdmin")
    public Msg updateAdmin(@RequestBody User user,HttpServletRequest request){
        HttpSession session = request.getSession();
        //判断登录状态
        try {
            User user1 = (User) session.getAttribute("AdminUser");
            user1.getId();
        }catch (Exception e){
            return Msg.error(999,"登录超时");
        }

        //判断状态码的值
        Integer userStatus = user.getUserStatus();
        if (userStatus==null){
            user.setUserStatus(0);
        }else if (!(0==userStatus||1==userStatus)){
            return Msg.error(1,"用户账号状态码有误");
        }
        //修改用户
        boolean bl = iUserService.updateAdmin(user);
        if (bl){
            return Msg.success("SUCCESS");
        }
        return Msg.error(1,"failed");
    }




    /**
     * @api {get} /user/getUsers  后台查询所有用户列表
     * @apiDescription 后台查询所有用户列表&nbsp;&nbsp;<span style="color:#D0254E"><i>[*需要在管理员登陆状态下访问]</i></span>
     *       <br/><span style="color:#012456"><b>作者:何龙</b></span>
     * @apiGroup User API
     *
     * @apiParam {int} [page=1] 页码数
     * @apiParam {int} [rows=5] 每页数据条数
     * @apiParam {int} [roler=0] 账号类型
     *                  <br/>0:管理员
     *                  <br/>1:普通用户
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     *          "page": 1,
     *          "rows": 2,
     *      }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccess  {int} code 返回状态码 <br/> <span style="color:#1AA034">0:正常 </span>
     *                                    <br/> <span style="color:red">999:登陆超时!</span>
     * @apiSuccess  {string} userId 用户编号
     * @apiSuccess  {string} userName 用户名
     * @apiSuccess  {string} userAddressProvince 省
     * @apiSuccess  {string} userAddressCity 市
     * @apiSuccess  {string} userAddressArea 区镇
     * @apiSuccess  {string} userAddressStreet 具体住址
     * @apiSuccess  {string} userPhone 电话
     * @apiSuccess  {int} rolerId 角色id
     *                    <br/>2:管理员
     *                    <br/>3:普通用户
     * @apiSuccess  {string} userEmail 邮箱
     * @apiSuccess  {string} userStatus 账号状态
     *                    <br/>1:正常
     *                    <br/>0:账号被冻结
     *
     * @apiSuccessExample {json} 正确返回值:
     *
     *{
     *     "code": 0,
     *     "msg": "",
     *     "data": {
     *         "total": 5,
     *         "list": [
     *             {
     *                 "id": 1,
     *                 "userId": "110",
     *                 "userName": "zhangsan",
     *                 "userPass": "E10ADC3949BA59ABBE56E057F20F883E",
     *                 "realName": "张三",
     *                 "userAddressProvince": "陕西",
     *                 "userAddressCity": "西安",
     *                 "userAddressArea": "雁塔",
     *                 "userAddressStreet": "丈八",
     *                 "userPhone": "11111201234",
     *                 "rolerId": 3,
     *                 "userEmail": "11111201234@163.com",
     *                 "userPayPass": "666666",
     *                 "userStatus": 1
     *             },
     *             {
     *                 "id": 4,
     *                 "userId": "111155",
     *                 "userName": "hll",
     *                 "userPass": "E10ADC3949BA59ABBE56E057F20F883E",
     *                 "realName": "",
     *                 "userAddressProvince": null,
     *                 "userAddressCity": null,
     *                 "userAddressArea": null,
     *                 "userAddressStreet": null,
     *                 "userPhone": null,
     *                 "rolerId": 3,
     *                 "userEmail": "741920@qq.com",
     *                 "userPayPass": null,
     *                 "userStatus": 1
     *             }
     *         ]
     *     }
     * }
     *
     * @apiErrorExample {json} 错误返回值:
     *     {
     *     "code": 999,
     *     "msg": "登陆超时!",
     *     "data": null
     *     }
     *
     * @apiSampleRequest /user/getUsers
     */
    @GetMapping("/getUsers")
    public Msg getUsers(@RequestParam(value = "page",defaultValue = "1")Integer page,
                        @RequestParam(value = "rows",defaultValue = "5")Integer rows,
                        @RequestParam(value = "roler",defaultValue = "0")Integer roler,
                        HttpSession session){
        try {
            //验证是否登录
            User user = (User) session.getAttribute( "AdminUser" );
            user.getId();
        }catch (Exception e){
            return Msg.error( 999,"登录超时!" );
        }
        page=page<1?1:page;



        ReturnList list=iUserService.getUsers(page,rows,roler);
        return Msg.success(list);
    }

}
